package com.ebelter.nb.utils;


import android.text.TextUtils;
import com.ebelter.btcomlib.models.bluetooth.bean.ProductStyle;
import com.ebelter.btcomlib.models.bluetooth.products.nb.BondDevices;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.activity_manager.ActivityManagers;
import com.ebelter.nb.R;
import com.ebelter.nb.constants.NbConstants;
import com.ebelter.nb.model.dao.DbUtils;
import com.ebelter.nb.ui.activitys.AddDeviceActivity;
import com.ebelter.nb.ui.activitys.AddDeviceResultActivity;
import com.ebelter.nb.ui.activitys.AddInfoActivity;
import com.ebelter.nb.ui.activitys.DeviceManagerActivity;
import com.ebelter.nb.ui.activitys.LoginActivity;
import com.ebelter.nb.ui.activitys.SetPswActivity;
import com.ebelter.nb.ui.view.CombinView5;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class NbUtitls {



    //获取正在登陆的用户id
    public static String getLoginUserId(){
        return UserSpUtil.readString(NbConstants.User.phone,null);
    }

    public static String getLoginAuthkey(){
        return UserSpUtil.readString(NbConstants.User.authkey,"");
    }

    public static int getLastDisplayFragment(){
        return UserSpUtil.readInt(NbConstants.System.LastDisplayFragment,0);//0是血压计 1是体脂秤
    }

    //判断是否绑定了血压计
    public static boolean isBondBpmDevice(){

        String loginUserId  = getLoginUserId();
        if (TextUtils.isEmpty(loginUserId)){
            return false;
        }
        List<BondDevices> bondDevices = DbUtils.queryBondedDevices(loginUserId);

        for (BondDevices bond:bondDevices){
            if (bond==null) continue;
            if (bond.type==0){//0为血压计
                return true;
            }
        }
        return false;
    }

    //0血压计 1体脂秤
    public static void removeBondDevice(int type){

        String loginUserId  = getLoginUserId();
        if (TextUtils.isEmpty(loginUserId)){
            return;
        }
        List<BondDevices> bondDevices = DbUtils.queryBondedDevices(loginUserId);

        for (int i=0;i<bondDevices.size();i++){
            BondDevices bond =bondDevices.get(i);
            if (bond==null) continue;
            if (bond.type==type){//0为血压计
                bondDevices.remove(bond);
                bond.delete();
                i--;
            }
        }
    }


    //判断是否绑定了体脂秤
    public static boolean isBondScaleDevice(){
        String loginUserId  = getLoginUserId();
        if (TextUtils.isEmpty(loginUserId)){
            return false;
        }
        List<BondDevices> bondDevices = DbUtils.queryBondedDevices(loginUserId);
        for (BondDevices bond:bondDevices){
            if (bond==null) continue;
            if (bond.type==1){//1为体脂秤
                return true;
            }
        }
        return false;
    }
    public static BondDevices getBondScaleDevice(){
        String loginUserId  = getLoginUserId();
        if (TextUtils.isEmpty(loginUserId)){
            return null;
        }
        List<BondDevices> bondDevices = DbUtils.queryBondedDevices(loginUserId);
        for (BondDevices bond:bondDevices){
            if (bond==null) continue;
            if (bond.type==1){//1为体脂秤
                return bond;
            }
        }
        return null;
    }

    public static BondDevices getBondBpmDevice(){
        String loginUserId  = getLoginUserId();
        if (TextUtils.isEmpty(loginUserId)){
            return null;
        }
        List<BondDevices> bondDevices = DbUtils.queryBondedDevices(loginUserId);
        for (BondDevices bond:bondDevices){
            if (bond==null) continue;
            if (bond.type==0){//1为体脂秤
                return bond;
            }
        }
        return null;
    }

    public static List<BondDevices> queryBondedDevices(){
        String loginUserId  = getLoginUserId();
        if (TextUtils.isEmpty(loginUserId)){
            return null;
        }
        return DbUtils.queryBondedDevices(loginUserId);
    }


    public static boolean queryIsBonded(ProductStyle curRunProductStyle){
        if (ProductStyle.BPM==curRunProductStyle){
            return NbUtitls.isBondBpmDevice();
        }else if (ProductStyle.SCALE==curRunProductStyle){
            return NbUtitls.isBondScaleDevice();
        }
        return false;
    }



    //判断资料是否完全
    public static boolean infoIsWanQuan(){
        String birthday = UserSpUtil.readString(NbConstants.User.birthday,null);
        int height = UserSpUtil.readInt(NbConstants.User.height,-1);
        int sex = UserSpUtil.readInt(NbConstants.User.sex,-1);
        float weight = UserSpUtil.readFloat(NbConstants.User.weight);
        int profession = UserSpUtil.readInt(NbConstants.User.profession,-1);
        return (!TextUtils.isEmpty(birthday))&&height>0&&sex>=0&&profession>=0&&weight>0;
    }

    public static int getHeight() {
        return UserSpUtil.readInt(NbConstants.User.height,-1);
    }

    //0男1女
    public static int getSex() {
        return UserSpUtil.readInt(NbConstants.User.sex,0);
    }

    public static int getProfession() {
        //0非体力劳动者 1体力劳动者 2非运动员 3运动员
        return UserSpUtil.readInt(NbConstants.User.profession,0);
    }

    public static int getAge() {

        String birthday = UserSpUtil.readString(NbConstants.User.birthday,null);//yyyy-MM-dd
        if (TextUtils.isEmpty(birthday)){
            return 1;
        }
        Date dateOfBirth = TimeUtils.parseFormatter3Time(birthday);
        int age = 1;
        Calendar born = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        if (dateOfBirth != null) {
            now.setTime(new Date());
            born.setTime(dateOfBirth);

            age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
            if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) {
                age -= 1;
            }
        }

        if (age<=0){//简单的修复一下岁数等于零的问题
            age=1;
        }
        return age;
    }




    public static void setCb5Value(CombinView5 cb5,int index,String value){
        if (cb5==null)return;
        if (index==1){
            cb5.setTitle1(value);
        }else if (index==2){
            cb5.setTitle2(value);
        }else if (index==3){
            cb5.setTitle3(value);
        }
    }




    public static int getDisplaySrcRes(float avgValue,float newstValue){

        avgValue = NumUtils.numBaoLiuWei1(avgValue);
        newstValue = NumUtils.numBaoLiuWei1(newstValue);

        if (avgValue==newstValue&&avgValue!=0){
            return R.drawable.ic_hold;
        }else if (avgValue<newstValue){
            return R.drawable.ic_shangshen;
        }else if (avgValue>newstValue){
            return R.drawable.ic_xiajiang;
        }
        return 0;
    }


}
